package com.junsi.web.controller.system;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.junsi.common.annotation.Log;
import com.junsi.common.core.controller.BaseController;
import com.junsi.common.core.domain.AjaxResult;
import com.junsi.common.core.page.TableDataInfo;
import com.junsi.common.enums.BusinessType;
import com.junsi.common.utils.poi.ExcelUtil;
import com.junsi.framework.util.ShiroUtils;
import com.junsi.system.domain.SysArea;
import com.junsi.system.domain.SysCity;
import com.junsi.system.service.ISysAreaService;
import com.junsi.system.service.ISysCityService;
import com.junsi.system.vo.SysAreaCity;
import com.junsi.system.vo.SysCityVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 省市区 信息操作处理
 * 
 * @author ymx
 * @date 2019-05-30
 */
@Api("省市区配置")
@Controller
@RequestMapping("/system/city")
public class SysCityController extends BaseController
{
	private String prefix = "system/city";

	@Autowired
	private ISysCityService cityService;
	@Autowired
	private ISysAreaService sysAreaService;

	@RequiresPermissions("system:city:view")
	@GetMapping()
	public String city(ModelMap mmap)
	{
		SysArea area = new SysArea();
		mmap.put("area", area);
		return prefix + "/city";
	}

	/**
	 * 查询省市区列表
	 */
	@ApiOperation("查询省市区列表")
	@ApiImplicitParam(name = "city", value = "城市信息", dataType = "SysCity")
	@RequiresPermissions("system:city:list")
	@GetMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysCity city)
	{
		startPage();
		List<SysCity> list = cityService.selectCityList(city);
		return getDataTable(list);
	}

	/**
	 * 查询省份列表
	 */
	@ApiOperation("查询省份列表")
	@GetMapping("/selectPro")
	@ResponseBody
	public  List<SysCity> selectDeptTree()
	{
		return  cityService.selectCityByPRo();
	}


	/**
	 * 导出省市区列表
	 */
	@ApiOperation("导出省市区列表")
	@ApiImplicitParam(name = "city", value = "城市信息", dataType = "SysCity")
	@RequiresPermissions("system:city:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(SysCity city)
	{
		List<SysCity> list = cityService.selectCityList(city);
		ExcelUtil<SysCity> util = new ExcelUtil<SysCity>(SysCity.class);
		return util.exportExcel(list, "city");
	}

	/**
	 * 新增省市区
	 */
	@ApiOperation("新增省市区")
	@RequiresPermissions("system:city:add")
	@ApiImplicitParam(name = "areaCode", value = "组织编码", dataType = "String")
	@GetMapping("/add")
	public String add(String areaCode, ModelMap mmap)
	{
		if (null != areaCode && !"".equals(areaCode)) {
			SysArea area = sysAreaService.selectAreaById(areaCode);
			List<SysAreaCity> cityList = sysAreaService.selectAreaAndCity(areaCode);
			mmap.put("area", area);
			mmap.put("cityList", cityList);
		}else {
			SysArea area = new SysArea();
			mmap.put("area", area);
		}
		return prefix + "/city";
	}

	/**
	 * 新增保存省市区
	 */
	@ApiOperation("新增保存省市区")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "cityCode", value = "城市编码", dataType = "String"),
		@ApiImplicitParam(name = "loginName", value = "当前用户", dataType = "String"),
		@ApiImplicitParam(name = "name", value = "基础设置类型", dataType = "String"),
		@ApiImplicitParam(name = "areaName", value = "所属大区", dataType = "String"),
		@ApiImplicitParam(name = "status", value = "状态", dataType = "String")
	})
	@Log(title = "省市区", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(String status, String areaName, String areaCode
			, String parentCode, String submitParentsCodes, String loginName)
	{		
		loginName=ShiroUtils.getLoginName();
		return cityService.addSave(status, areaName, areaCode, parentCode, submitParentsCodes, loginName);
	}

	/**
	 * 修改省市区
	 */
	@ApiOperation("修改省市区")
	@ApiImplicitParam(name = "areaCode", value = "区域编码", dataType = "String")
	@GetMapping("/edit/{areaCode}")
	public String edit(@PathVariable("areaCode") String areaCode)
	{
		return prefix + "/edit";
	}

	/**
	 * 修改保存省市区
	 */
	@ApiOperation("修改保存省市区")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "city", value = "城市信息", dataType = "SysCity"),
		@ApiImplicitParam(name = "cityCode", value = "城市编码", dataType = "String"),
	})
	@RequiresPermissions("system:city:edit")
	@Log(title = "省市区", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(SysCity city,String cityCode)
	{		
		return toAjax(cityService.editSave(city,cityCode));
	}

	/**
	 * 删除省市区
	 */
	@ApiOperation("删除省市区")
	@ApiImplicitParam(name = "ids", value = "城市编码", dataType = "String")
	@RequiresPermissions("system:city:remove")
	@Log(title = "省市区", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(cityService.deleteCityByIds(ids));
	}

	/**
	 * 查询区域城市列表
	 */
	@ApiOperation("查询区域城市列表")
	@ApiImplicitParam(name = "areaCode", value = "区域编码", dataType = "String")
	@GetMapping("/cityList")
	@ResponseBody
	public  List<SysCityVo> cityList(String areaCode)
	{
		List<SysCityVo> cityList = cityService.cityList(areaCode);
		return  cityList;
	}

}
